package com.tyc.springframework.jvm.jvmthorwable;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 直接内存溢出(非运行时数据区，使用本地函数库直接分配堆外内存)
 * -XX:MaxDirectMemorySize (默认与Xms值一致)
 * -Xms20M -XX:MaxDirectMemorySize=10M
 */
public class DirectMemoryOOM {
    private static final int _1M = 1024 * 1024;

    public static void main(String[] args) throws IllegalAccessException {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        while (true){
            unsafe.allocateMemory(_1M);
        }
    }
}
